/**
 * Created by macbookair on 2019/5/9.
 */
$(function () {



//轮播图------------------------------------------------------------------------
    //1.setInterval
//            var interval = setInterval(function () {
//                console.log(new Date());
//            },1000);
    //2.animate  $(selector).animate(obj,time,callback);
    //3.移入移除   fadein fadeout

    //轮播图参数设置
    var width = 720;
    var animateSpeed = 1000;
    var pause = 3000;
    var $slider = $('.slider');
    var $sliderContainer = $slider.find('.slides');
    var $slides = $sliderContainer.find('.slide');

    var currentSlide = 1;
    var interval;


    //鼠标移入移出事件
    $slider.on('mouseenter', pauseSlider).on('mouseleave', startSlider);

    function startSlider() {
        interval = setInterval(function () {
            $sliderContainer.animate({'margin-left': '-=' + width},
                animateSpeed, function () {
                    currentSlide++;
                    if (currentSlide === $slides.length) {
                        currentSlide = 1;
                        $sliderContainer.css('margin-left', 0);
                    }
                });
        }, pause);
    }

    function pauseSlider() {
        clearInterval(interval);
        interval = null;
    }

    startSlider();


    //登陆注册和验证----------------------------------------------------------------------
    //登陆按钮
    $('.navu').css('display','block');
    $('.navr').css('display','none');


    $('.btn-toregist').on('click',function () {
        window.location.href="/cl/login";
    });

    $('.btn-regist').on('click',function () {
        var username = $('.regist-username').val();
        var nickname =$('.regist-nickname').val();
        var password = $('.regist-password').val();
        var passwordsec = $('.regist-passwordsec').val();
        if (password==passwordsec){
            console.log(password)
            console.log(passwordsec)
            console.log(password==passwordsec)
            var user = {
                username : username,
                nickname : nickname,
                password : passwordsec
            }
            $.ajax({
                type:'POST',
                url:'/cl/doRegist',
                contentType: "application/json",
                data: JSON.stringify(user),
                success:function (res) {
                    if (res.code==1) {
                        $('.navr').hide();
                        $('.navu').fadeIn(100);
                        alert('注册成功');
                        window.location.href="/cl/firstpage"
                    }else {
                        $('.regist-error').css("用户名已存在，请重命名");
                    }
                }
            })
        }else {
            $('.regist-error').css('display','block');
        }

    });

    $('.btn-login').on('click',function () {
        var un = $('.username').val();
        var pw = $('.password').val();
        var user = {
             username: un,
             password: pw
        }
        $.ajax({
            type:'POST',
            url:'/cl/doLogin',
            contentType: "application/json",
            data:JSON.stringify(user),
            success:function (user) {
                if (user.code==1){
                $('.nav').hide();
                $('.navu').fadeIn(100);
                window.location.href="/cl/firstpage"
                }
                else {
                    $('.login-error').css('display','block');
                }
            }
        });


    })



    //截取文本处理----------------------------------------------------------------------
    /**
     * 截取文本
     * @param a 文本内容
     * @param b 截取字符长度
     * @param c 后缀,默然""
     */

    $(".substring").each(function() {
        var len = $(this).text().length;   //当前HTML对象text的长度
        // console.log(len);
        // console.log($(this).text());
        if (len > 200) {
            var str = "";
            str = $(this).text().substring(0, 170) + "...";  //使用字符串截取，获取前30个字符，多余的字符使用“......”代替
            // console.log(str);
            $(this).html(str);                   //将替换的值赋值给当前对象
        }
    });

    $(".substring-title").each(function() {
        var len = $(this).text().length;   //当前HTML对象text的长度
        // console.log(len);
        // console.log($(this).text());
        if (len > 18) {
            var str = "";
            str = $(this).text().substring(0, 18) + "...";  //使用字符串截取，获取前30个字符，多余的字符使用“......”代替
            // console.log(str);
            $(this).html(str);                   //将替换的值赋值给当前对象
        }
    });

    //投票-------------------------------------------------


    $.ajax({
        type:'GET',
        url:'/cl/getVotes',

        success:function (data) {
             //登录后
            $.each(data,function (i,votes) {

                //投过票
                //未投过票
                 if (votes.isvote=="yes") {
                     //投过票，直接显示，不添加点击事件
                     console.log("此投票已经投过票了");
                     //填充html
                                    var vid=votes.vote.vid;
                                    var vname=votes.vote.vtitle;
                                    var str= "<li style=\"display: contents\">\n" +
                                        "<div class=\"votes\" id=\""+vid+"\">\n" +
                                        "<div class=\"vote-title\">"+vname+"</div>";
                                    var votel = votes.voteselect;
                                    $.each(votel,function (i,voteselect) {
                                        var vsid= voteselect.vsid;
                                        str=str+" <div class=\"skillbar clearfix\" id=\""+vsid+"\" data-percent=\""+votes[vsid]+"\" name=\"html\">\n" +
                                            "                        <div class=\"skillbar-title\"><span>"+voteselect.vsname+"</span></div>\n" +
                                            "                        <div class=\"skillbar-bar\" ></div>\n" +
                                            "                        <div class=\"skill-bar-percent\"></div>\n" +
                                            "                    </div>";

                                    })
                                          str=str+"      </div>\n" +
                                                "            </li>";
                            $('#votelist').append(str);
                             $.each(votel,function (i,voteselect) {
                                 var vsid= voteselect.vsid;
                                 var vs=$('#votelist').find('#'+vsid);
                                 console.log(vs);
                                 var vsperc=$('#votelist').find('#'+vsid).attr('data-percent');
                                 console.log(vsid+":"+vsperc);
                                 vs.parent().find('.skillbar').css({
                                     background:'#eee'
                                 });
                                 vs.find('.skillbar-bar').animate({
                                     width:vsperc
                                 },1000);
                                 vs.find('.skill-bar-percent').html(vsperc);
                             })
                     //直接显示，添加动画效果
                 }else {
                     //未投过票，不显示，添加点击事件
                     console.log("次投票未未未投过票");
                     //填充html
                     var vid=votes.vote.vid;
                     var vname=votes.vote.vtitle;
                     var str= "<li style=\"display: contents\">\n" +
                         "<div class=\"votes\" id=\""+vid+"\">\n" +
                         "<div class=\"vote-title\">"+vname+"</div>";
                     var votel = votes.voteselect;
                     $.each(votel,function (i,voteselect) {
                         var vsid= voteselect.vsid;
                         str=str+" <div class=\"skillbar clearfix\" id=\""+vsid+"\" data-percent=\""+votes[vsid]+"\" name=\"html\">\n" +
                             "                        <div class=\"skillbar-title\"><span>"+voteselect.vsname+"</span></div>\n" +
                             "                        <div class=\"skillbar-bar\" ></div>\n" +
                             "                        <div class=\"skill-bar-percent\"></div>\n" +
                             "                    </div>";

                     })
                     str=str+"      </div>\n" +
                         "            </li>";
                     $('#votelist').append(str);

                     //添加点击事件
                     $('.skillbar').on('click',function () {
                         var sparent= $(this).parent();
                         var st= sparent.find('.skillbar');
                         $(this).css({
                             color:'white',
                             background:'#eee',
                             border:"2px solid #1193b5"
                         })
                         st.unbind('click');
                         var vsid=$(this).attr('id');
                         console.log(vid+"!!!");
                         $.ajax({
                             type:'POST',
                             url:"/cl/vote?vsid="+vsid,
                             success:function(data) {
                                 var skill=$(this).parent().find('.skillbar');
                                 $.each(skill,function (i,sk) {

                                     var vsid= $(sk).attr('id');
                                     var perc= data[vsid];
                                     console.log("vsid="+vsid+"perc="+perc);
                                     $(sk).attr('data-percent',perc);
                                 })
                             }
                         });
                         st.each(function () {
                             $(this).find('.skillbar-bar').animate({
                                 width:$(this).attr('data-percent')
                             },1000);
                             $(this).find('.skill-bar-percent').html($(this).attr('data-percent'));
                         })

                     })
                 }
            })

            //未登录，直接全部显示
        }
    })

  /*  $('#search-btn').on('click',function () {
        var searchcontent= $('#search-input').val();
        console.log(searchcontent);
        var kv= {input:searchcontent};
        console.log(kv);
        $.ajax({
            type:'POST',
            url:'/cl/searchArticle?input='+searchcontent
        })
    });*/


});